Skip to content

[code-simplifier] refactor: simplify nested ternary and duplicate assignments in recent changes#9477

Merged
Evangelink merged 1 commit into
mainfrom
code-simplifier/simplify-recent-changes-20260627-f5b59a3389bd8ff9
Jun 28, 2026
Merged

[code-simplifier] refactor: simplify nested ternary and duplicate assignments in recent changes#9477
Evangelink merged 1 commit into
mainfrom
code-simplifier/simplify-recent-changes-20260627-f5b59a3389bd8ff9

Conversation

@Evangelink

Copy link
Copy Markdown
Member

Code Simplification — 2026-06-27

This PR simplifies recently modified code to improve clarity, consistency, and maintainability while preserving all functionality.

Files Simplified

  • src/Adapter/MSTestAdapter.PlatformServices/MSTestSettings.Configuration.cs — Replace nested ternary chain with an if/else chain
  • src/Platform/Microsoft.Testing.Extensions.VideoRecorder/VideoRecorderSessionHandler.cs — Eliminate duplicate field assignments in constructor

Improvements Made

1. Removed Nested Ternary (MSTestSettings.Configuration.cs)

The parallelism:workers parsing in SetSettingsFromConfig used a nested ternary chain:

// Before — nested ternary (violates project guidelines)
settings.ParallelizationWorkers = int.TryParse(workers, out int parallelWorkers)
    ? parallelWorkers == 0
        ? Environment.ProcessorCount
        : parallelWorkers > 0
            ? parallelWorkers
            : throw new AdapterSettingsException(...)
    : throw new AdapterSettingsException(...);
// After — if/else chain
if (!int.TryParse(workers, out int parallelWorkers) || parallelWorkers < 0)
{
    throw new AdapterSettingsException(...);
}
settings.ParallelizationWorkers = parallelWorkers == 0 ? Environment.ProcessorCount : parallelWorkers;

The project guidelines explicitly state: "Avoid nested ternary operators — prefer switch statements or if/else chains." This file was modified by PR #9453 (merged 2026-06-26).

2. Eliminated Duplicate Assignments (VideoRecorderSessionHandler.cs)

The constructor (new file from PR #9377, merged 2026-06-26) set _persistMode and _granularity in both the enabled and disabled branches:

// Before — duplicate assignments in two branches
if (!_enabled)
{
    _persistMode = options.PersistMode;   // ← duplicate
    _granularity = options.Granularity;   // ← duplicate
    return;
}
ApplyCommandLineOverrides(options, commandLineOptions);
_persistMode = options.PersistMode;       // ← duplicate
_granularity = options.Granularity;       // ← duplicate
// After — single assignment after the conditional
if (_enabled)
{
    ApplyCommandLineOverrides(options, commandLineOptions);
}
_persistMode = options.PersistMode;
_granularity = options.Granularity;
if (!_enabled)
{
    return;
}

Changes Based On

Recent changes from:

Testing

  • MSTestAdapter.PlatformServices builds cleanly (0 warnings, 0 errors) for net8.0 and net9.0
  • Microsoft.Testing.Extensions.VideoRecorder builds cleanly (0 warnings, 0 errors) for net8.0, net9.0, and netstandard2.0
  • ✅ No functional changes — behavior is identical in both cases

Review Focus

Please verify:

  • The if/else refactor preserves the original parallelism:workers validation (parse failure or negative → throw, 0ProcessorCount, positive → value)
  • The constructor reorder correctly defers _recorder creation to when _enabled is true

🤖 Automated content by GitHub Copilot. Posted via a maintainer's GitHub token, so it appears under their account — the account owner did not write or approve this content personally. Generated by the Code Simplifier workflow. · 252.5 AIC · ⌖ 23.5 AIC · ⊞ 9.3K · [◷]( · )

Add this agentic workflows to your repo

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/code-simplifier.md@main
  • expires on Jun 28, 2026, 1:55 PM UTC

… changes

- MSTestSettings.Configuration.cs: replace nested ternary chain for
  parallelism:workers parsing with an if/else chain; project guidelines
  explicitly forbid nested ternaries (prefer if/else or switch).
  Behavior is identical: parse failure or negative value throws, 0 maps
  to ProcessorCount, positive value is used as-is.

- VideoRecorderSessionHandler.cs: eliminate duplicate _persistMode /
  _granularity assignments in the constructor. Both branches set the same
  fields from options; extracted them to a shared assignment after the
  conditional ApplyCommandLineOverrides call.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 27, 2026 13:55
@Evangelink Evangelink added type/automation Created or maintained by an agentic workflow. type/tech-debt Code health, refactoring, simplification. labels Jun 27, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR performs small refactors in two recently changed areas to improve readability and reduce duplication while preserving existing behavior in MSTest adapter configuration parsing and the VideoRecorder MTP extension.

Changes:

  • Replaced a nested ternary chain with an explicit parse/validate if guard for mstest:parallelism:workers.
  • Removed duplicate _persistMode / _granularity assignments in VideoRecorderSessionHandler by centralizing them after the conditional override logic.
Show a summary per file
File Description
src/Adapter/MSTestAdapter.PlatformServices/MSTestSettings.Configuration.cs Simplifies parallelism:workers parsing while preserving: parse failure/negative ⇒ throw; 0 ⇒ Environment.ProcessorCount; positive ⇒ value.
src/Platform/Microsoft.Testing.Extensions.VideoRecorder/VideoRecorderSessionHandler.cs Avoids duplicated field assignments by applying CLI overrides only when enabled, then assigning _persistMode/_granularity once before the disabled early-return.

Review details

  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Low

@Evangelink Evangelink marked this pull request as ready for review June 28, 2026 04:52
@Evangelink Evangelink added the state/needs-review Awaiting review from the team. label Jun 28, 2026

@Evangelink Evangelink left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

🤖 Automated review by GitHub Copilot. Posted via a maintainer's GitHub token, so it appears under their account — the account owner did not write or approve this content personally. Generated by the Expert Code Review workflow. To request a follow-up action, reply by tagging @copilot directly.

✅ 22/22 dimensions clean — no findings.


Behavior-equivalence trace

MSTestSettings.Configuration.cs

The original nested ternary had four execution paths; the refactored guard+ternary covers them identically:

Input Original Refactored
int.TryParse fails outer throw !int.TryParse(...)throw
parallelWorkers < 0 innermost throw parallelWorkers < 0throw
parallelWorkers == 0 Environment.ProcessorCount ternary true-branch
parallelWorkers > 0 parallelWorkers ternary false-branch

Both throw the same AdapterSettingsException with the same message format for both invalid cases. Existing tests GetSettingsShouldThrowIfParallelizationWorkersIsNotInt (line 409), GetSettingsShouldThrowIfParallelizationWorkersIsNegative (line 428), ParallelizationWorkersShouldBeConsumedFromRunSettingsWhenSpecified (line 446), and ParallelizationWorkersShouldBeSetToProcessorCountWhenSetToZero (line 464) in MSTestSettingsTests.cs exercise every branch.

VideoRecorderSessionHandler.cs

ApplyCommandLineOverrides mutates options.PersistMode and options.Granularity based on CLI arguments. Both the old and new code ensure the assignments _persistMode = options.PersistMode and _granularity = options.Granularity happen after the call when _enabled == true, preserving the ordering invariant. When _enabled == false neither version calls ApplyCommandLineOverrides, so the unmodified option defaults are captured in both cases.


N/A dimensions (8): 10 Test Isolation, 11 Assertion Quality, 12 Flakiness Patterns, 14 Data-Driven Coverage, 18 Analyzer Quality, 19 IPC Wire Compatibility, 20 Build Infrastructure, 22 PowerShell Scripting Hygiene.

@github-actions

Copy link
Copy Markdown
Contributor

This pull request was automatically closed because it expired on 2026-06-28T13:55:40.146Z.

Closed by Workflow

@github-actions github-actions Bot closed this Jun 28, 2026
@Evangelink Evangelink reopened this Jun 28, 2026
@Evangelink Evangelink merged commit 311f97d into main Jun 28, 2026
53 checks passed
@Evangelink Evangelink deleted the code-simplifier/simplify-recent-changes-20260627-f5b59a3389bd8ff9 branch June 28, 2026 19:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

state/needs-review Awaiting review from the team. type/automation Created or maintained by an agentic workflow. type/tech-debt Code health, refactoring, simplification.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants